package com.yanze.customviewdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.yanze.customviewdemo.widget.ActivityProvider;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.ThreadPoolExecutor;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Activity activity = ActivityProvider.getActivity();
        if (activity != null) {
            startActivity(new Intent(this,activity.getClass()));
        }
    }

    public void textOnclick(View view) {
        switch (view.getId()) {
            case R.id.btn_proxy:
                proxy();
                break;
            case R.id.btn_tran:
                int a = 3;
                int b = 4;
                double z = tranLine(3, 4);
                Toast.makeText(this, "第三边：" + z, Toast.LENGTH_SHORT).show();
                break;
            case R.id.location_mark:
                startActivity(new Intent(this,TaijiActivity.class));
                break;
            case R.id.btn_kotlin:
                break;
        }
    }

    public double tranLine(float x, float y) {
        return Math.sqrt((Math.pow(x,2) + Math.pow(y,2)));
    }


    class NormalHandler implements InvocationHandler {
        private Object target;

        public NormalHandler(Object target) {
            this.target = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Log.i("yanze_proxy", "man say invoke: " + System.currentTimeMillis());
            method.invoke(target, args);
            return null;
        }
    }

    interface IPerson {
        void say();
    }

    class Man implements IPerson {
        @Override
        public void say() {
            Log.i("yanze_proxy", "man say:shit");
        }
    }

    // 动态代理
    public void proxy(){
        Man man = new Man();
        NormalHandler normalHandler = new NormalHandler(man);
        IPerson iPerson = (IPerson) Proxy.newProxyInstance(IPerson.class.getClassLoader()
                , new Class[]{IPerson.class}, normalHandler);
        iPerson.say();
    }

    public void test() {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
//        HandlerThread();
//        ThreadPoolExecutor.DiscardOldestPolicy
    }


}
